package com.example.lawyer.netty;

import com.example.lawyer.netty.handler.NettyMessageHandler;
import com.example.lawyer.netty.handler.TcpMessageDecoder;
import com.example.lawyer.netty.handler.TcpMessageEncoder;
import com.example.lawyer.service.NettyService;

import java.util.concurrent.TimeUnit;

import io.netty.channel.ChannelInitializer;
import io.netty.channel.ChannelPipeline;
import io.netty.channel.socket.SocketChannel;
import io.netty.handler.timeout.IdleStateHandler;

public class NettyInit extends ChannelInitializer<SocketChannel> {

    private NettyService service;
    public NettyInit(NettyService service) {
        this.service = service;
    }

    @Override
    public void initChannel(SocketChannel ch) throws Exception {
        ChannelPipeline pipeline = ch.pipeline();
        pipeline.addLast("decode", new TcpMessageDecoder());
        pipeline.addLast("encode", new TcpMessageEncoder());
        pipeline.addLast(new IdleStateHandler(60,60,60, TimeUnit.SECONDS));
        pipeline.addLast("handler", new NettyMessageHandler(service));

    }
}
